/**
 * @file StoreAdapter.java
 * @create 下午4:38:37
 * @author CX
 * @description StoreFragmentAdapter  stroefragment的适配器
 */
package cn.edu.hstc.wm.adapter;

import java.util.Collections;
import java.util.LinkedList;
import java.util.List;

import android.content.Context;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.TextView;
import cn.edu.hstc.wm.AppConfigs;
import cn.edu.hstc.wm.Constants;
import cn.edu.hstc.wm.R;
import cn.edu.hstc.wm.bean.Store;
import cn.edu.hstc.wm.service.AppConfigService;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.ImageLoadingListener;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;
import com.nostra13.universalimageloader.core.assist.SimpleImageLoadingListener;
import com.nostra13.universalimageloader.core.display.FadeInBitmapDisplayer;
import com.nostra13.universalimageloader.core.display.RoundedBitmapDisplayer;

public final class StoreFragmentAdapter extends BaseAdapter {
	public static final String TAG = "StoreAdapter";
	/** 店铺集合 */
	List<Store> stores;
	Context mContext;
	ImageLoader mImageLoader;
	DisplayImageOptions options;
	ViewHolder holder;

	private ImageLoadingListener animateFirstListener = new AnimateFirstDisplayListener();

	public StoreFragmentAdapter(Context context, List<Store> stores) {
		this.mContext = context;
		this.stores = stores;
		mImageLoader = ImageLoader.getInstance();
		// 图片 显示的配置样式
		options = new DisplayImageOptions.Builder().showImageOnLoading(R.drawable.ic_loading_img)// 设置图片下载期间显示的图片
				.showImageForEmptyUri(R.drawable.ic_no_img)// 设置图片Uri为空或是错误的时候显示的图片
				.showImageOnFail(R.drawable.ic_no_img)// 设置图片加载或解码过程中发生错误显示的图片
				.cacheInMemory(true) // 设置下载的图片是否缓存在内存中
				.cacheOnDisc(true)// 设置下载的图片是否缓存在SD卡中
				.considerExifParams(true).displayer(new RoundedBitmapDisplayer(5))// 设置成圆角图片
				.build();// 创建配置过得DisplayImageOption对象
	}

	@Override
	public int getCount() {
		return stores.size();
	}

	@Override
	public Object getItem(int position) {
		return stores.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// Log.d(TAG, "getView() " + position);
		if (convertView == null) {
			convertView = LayoutInflater.from(mContext).inflate(R.layout.store_fragment_item, null);
			holder = new ViewHolder();
			initView(holder, convertView);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		// 设置各种参数
		setView(holder, stores.get(position));
		return convertView;
	}

	/**
	 * 初始化各个view
	 * 
	 * @param holder
	 * @param convertView
	 */
	private void initView(ViewHolder holder, View convertView) {
		holder.name = (TextView) convertView.findViewById(R.id.tv_store_name);
		holder.grade = (RatingBar) convertView.findViewById(R.id.ratingbar_store_grade);
		holder.speed = (TextView) convertView.findViewById(R.id.tv_store_send_speed);
		holder.distance = (TextView) convertView.findViewById(R.id.tv_store_distance);

		holder.icon = (ImageView) convertView.findViewById(R.id.iv_store_icon);
		holder.phone = (ImageView) convertView.findViewById(R.id.iv_store_support_phone);
		holder.sms = (ImageView) convertView.findViewById(R.id.iv_store_support_sms);
		holder.reserve = (ImageView) convertView.findViewById(R.id.iv_store_support_reserve);
		holder.send = (ImageView) convertView.findViewById(R.id.iv_store_support_send);
	}

	/**
	 * 设置各个view
	 * 
	 * @param holder
	 * @param storeEntity
	 */
	private void setView(ViewHolder holder, Store storeEntity) {
		try {
			// 异步加载图片关键方法
			// ImageManager2.from(mContext).displayImage(holder.icon,
			// AppConfigs.IMAGE_URL + storeEntity.getImageName(),
			// R.drawable.ic_default_icon, 72, 72);
			// ImageManager2.from(mContext).displayImage(holder.icon,
			// storeEntity.getIconName(), R.drawable.ic_default_icon);
			// holder.icon.setImageResource(storeEntity.getIconName()); //异步加载图片
			// holder.icon.setImageURI(uri);
			// 系统配置为允许加载图片

			String imageName = storeEntity.getImageName();
			//holder.icon.setMaxHeight(72);
			//holder.icon.setMaxWidth(72);
			if (!imageName.equals(Constants.DEFAULT_IMG) && AppConfigService.isLoadImage(mContext)) {
				// 不是默认的则加载
				mImageLoader.displayImage(AppConfigs.STORE_IMAGE_URL + imageName, holder.icon, options, animateFirstListener);
			} else {
				// 如果是无图则加载本地的默认图片
				holder.icon.setImageResource(R.drawable.default_no_img);
			}

			holder.name.setText(storeEntity.getName());
			// 设置评分等级
			holder.grade.setRating((float) storeEntity.getScore());
			// 设置速度
			holder.speed.setText(String.valueOf(storeEntity.getSpeed()) + " min");
			// 设置距离
			holder.distance.setText(String.valueOf("0 m"));

			// 设置支持状态
			holder.phone.setEnabled(storeEntity.getSuport_call() == 1);
			holder.sms.setEnabled(storeEntity.getSuport_SMS() == 1);
			holder.reserve.setEnabled(storeEntity.getSuport_order() == 1);
			holder.send.setEnabled(storeEntity.getSuport_send() == 1);

		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	public final class ViewHolder {
		public TextView name;
		public ImageView icon;
		public RatingBar grade;
		public TextView speed;
		public TextView distance;

		/**
		 * 支持电话,短信,预订,外送
		 */
		public ImageView phone, sms, reserve, send;
	}

	private static class AnimateFirstDisplayListener extends SimpleImageLoadingListener {

		private static final List<String> displayedImages = Collections.synchronizedList(new LinkedList<String>());

		@Override
		public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
			if (loadedImage != null) {
				ImageView imageView = (ImageView) view;
				// 是否第一次显示
				boolean firstDisplay = !displayedImages.contains(imageUri);
				if (firstDisplay) {
					// 图片淡入效果
					FadeInBitmapDisplayer.animate(imageView, 500);
					displayedImages.add(imageUri);
				}
			}
		}
	}

}
